import 'package:easy_refresh/easy_refresh.dart';

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:sainbainu/common/models/response/content_response.dart';
import 'package:sainbainu/common/routes/routes.dart';
import 'package:sainbainu/common/widgets/widget.dart';
import 'package:nested_scroll_view_plus/nested_scroll_view_plus.dart';

import 'index.dart';
import 'widgets/widgets.dart';

class GroupDetailPage extends GetView<GroupDetailController> {
  const GroupDetailPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<GroupDetailController>(
      builder: (_) {
        return Scaffold(
          body: EasyRefresh.builder(
            controller: _.state.refreshController,
            onLoad: _.onRichBottom,
            childBuilder: (context, physics) {
              return _buildNestedScrollView(physics);
            },
          ),
        );
      },
    );
  }

  /// 主视图
  Widget _buildNestedScrollView(ScrollPhysics physics) {
    return NestedScrollViewPlus(
      controller: controller.state.scrollController,
      physics: physics,
      overscrollBehavior: OverscrollBehavior.outer,
      headerSliverBuilder: (context, innerScrolled) {
        return [
          CustomSliverHeader(
            onBack: () => controller.handleBack(),
            onJoinTap: () => controller.handleJoin(),
            onEditTap: () => controller.handleEdit(),
            onMemberTap: () => controller.toMemberPage(),
          ),
        ];
      },
      body: _buidBody(physics),
    );
  }

  /// 包装每一个视图
  Widget _buidBody(ScrollPhysics physics) {
    return GetBuilder<GroupDetailController>(
      id: 'contentList',
      builder: (_) => ListView.separated(
        physics: physics,
        itemCount: _.state.contentList.rows.length,
        padding: const EdgeInsets.all(0),
        separatorBuilder: (context, index) {
          return const SizedBox(height: 10);
        },
        itemBuilder: (context, index) {
          ContentResponse item = _.state.contentList.rows[index];

          var ipText = '';
          if ((item.provinceName != null && item.provinceName!.isNotEmpty) ||
              (item.cityName != null && item.cityName!.isNotEmpty)) {
            ipText = '${item.provinceName}${item.cityName}  ';
          }

          return GestureDetector(
            onTap: () {
              Get.toNamed(
                AppRoutes.contentDetail,
                arguments: {
                  'noteId': item.noteId,
                  'uniqueTag': item.uniqueTag,
                  'avatar': item.avatar,
                },
              );
            },
            child: Container(
              color: Colors.white,
              padding: const EdgeInsets.only(
                top: 20,
                left: 20,
                right: 20,
                bottom: 5,
              ),
              child: UserInfoCell(
                avatarHeroTag: '${item.uniqueTag}_${item.noteId}',
                avatar: item.avatar,
                name: item.customerName ?? '',
                showNameAuth: item.officialFlag == 1,
                nameAuthText: item.officialName,
                role: '$ipText${item.createTime ?? ''}',
                roleFontSize: 13,
                onUserTap: () => _.toUserDetailPage(item.customerId),
                bottomChild: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    if (item.text != null && item.text!.isNotEmpty)
                      Padding(
                        padding: const EdgeInsets.only(top: 6),
                        child: ContentCardText(item.text!),
                      ),
                    ImageVideoGrid(
                      padding: const EdgeInsets.only(top: 12),
                      imageLinkList: item.image ?? [],
                      videoLink: item.videoCover,
                    ),
                    if (item.audio != null && item.audio!.isNotEmpty)
                      Padding(
                        padding: const EdgeInsets.only(top: 12),
                        child: AudioWaveWidget(
                          duration: item.audioDuration ?? 0,
                        ),
                      ),
                    IconNumRow(
                      giveFlag: item.giveFlag ?? 0,
                      giveNum: item.giveNum ?? 0,
                      collectionFlag: item.collectionFlag ?? 0,
                      collectionNum: item.collectionNum ?? 0,
                      visitNum: item.visitNum ?? 0,
                      commentNum: item.commentNum ?? 0,
                      shareNum: item.shareNum ?? 0,
                      onLikeTap: (isGive) =>
                          _.onGiveHandle(isGive, item.noteId),
                      onCollectionTap: (isGive) =>
                          _.onCollectionHandle(isGive, item.noteId),
                    ),
                  ],
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
